<?php
$model_object = 'model_' . strtolower($_POST['path']) . "_" . $module_name_small;
$language_path = strtolower($_POST['path'] . '/' . $module_name_small);

$form_request_fields = array();
$form_error_fields = array();
$form_entry_fields = array();

$list_column_fields = array();
$list_db_fields = array();
$list_sort_fields = array();
$validate_fields = array();

$all_dropdown = array('status','select');
$dropdown_fields_in = $dropdown_fields = array();
foreach($module_fields as $each_field) {
	if(in_array($each_field['type'], $all_dropdown))
		$dropdown_fields[] = $each_field;
}
$dropdown_fields_in = get_names('name', $dropdown_fields);

$pk_field = array();
foreach($module_fields as $each_field) {
	if($each_field['pk']) {
		$pk_field = $each_field;
	}
}

$sort_fields = get_fields('sort', $module_fields);
$sort_fields = get_names('name', $sort_fields);
$list_fields = get_fields('list', $module_fields);
$form_fields = get_fields('form', $module_fields);
$require_fields = get_fields('require', $module_fields);
$search_fields = get_fields('search', $module_fields);
//$search_fields = get_names('name', $search_fields);
//print_r($dropdown_fields);
//print_r($module_fields);
//die;

ob_start();
		echo "\r" . '				\'' . $pk_field['name'] . '\' => $result[\'' . $pk_field['name'] . '\'],';
foreach ($list_fields as $each_field) {
	switch($each_field['type']) {
		case 'date_added':		 
		case 'date_modified':		 
		case 'datetime':		 
		case 'date':		 
		 	echo "\r" . '				\'' . $each_field['name'] . '\' => date($this->language->get(\'date_format_short\'), strtotime($result[\'' . $each_field['name'] . '\'])),';
		break;
		default:
			
			if(in_array($each_field['name'], $dropdown_fields_in)) {

				$field_name = $each_field['name'];
				if(preg_match("@_id@", $each_field['name'])) $field_name = str_replace("_id", "", $each_field['name']);				

				echo "\r" . '				\'' . $each_field['name'] . '\' => $display_' . $field_name . 's[$result[\'' . $each_field['name'] . '\']],';				
			} else {
				echo "\r" . '				\'' . $each_field['name'] . '\' => $result[\'' . $each_field['name'] . '\'],';
			}				
				
		break;
	}	
}
$list_db_fields = ob_get_clean();

ob_start();
foreach ($form_fields as $each_field) {
	echo "\r" . '		$this->data[\'entry_' . $each_field['name'] . '\'] = $this->language->get(\'entry_' . $each_field['name'] . '\');';
}
$form_entry_fields = ob_get_clean();

ob_start();
foreach ($dropdown_fields as $each_field) {
	echo "\r" . '		$this->data[\'text_' . $each_field['name'] . '\'] = $this->language->get(\'text_' . $each_field['name'] . '\');';
}

foreach ($list_fields as $each_field) {
	echo "\r" . '		$this->data[\'column_' . $each_field['name'] . '\'] = $this->language->get(\'column_' . $each_field['name'] . '\');';
}
$list_column_fields = ob_get_clean();

ob_start();
foreach ($list_fields as $each_field) {	
	if(in_array($each_field['name'], $sort_fields))
		echo "\r" . '		$this->data[\'sort_' . $each_field['name'] . '\'] = HTTPS_SERVER . \'index.php?route=' . $module_path . '&token=\' . $this->session->data[\'token\'] . \'&sort=' . $each_field['name'] . '\' . $url;';
	else
		echo "\r" . '		$this->data[\'sort_' . $each_field['name'] . '\'] = \'javascript:void(0)\';';
}
$list_sort_fields = ob_get_clean();

ob_start();
foreach ($require_fields as $each_field) {

	echo "\r" . '		if ((strlen(utf8_decode($this->request->post[\'' . $each_field['name'] . '\'])) <= 0)) {' . "\n" .
	'			$this->error[\'' . $each_field['name'] . '\'] = $this->language->get(\'error_' . $each_field['name'] . '\');' . "\n" .
	'		}' . "\n";
	
	if(in_array($each_field['type'], array('text_num'))) {
		echo "\r" . '		if (!isset($this->error[\'' . $each_field['name'] . '\']) && !is_int($this->request->post[\'' . $each_field['name'] . '\'])) {' . "\n" .
	'			$this->error[\'' . $each_field['name'] . '\'] = $this->language->get(\'error_invalid_' . $each_field['name'] . '\');' . "\n" .
	'		}' . "\n";		
	}
	
	if(in_array($each_field['type'], array('text_email'))) {
		echo "\r" . '		if (!isset($this->error[\'' . $each_field['name'] . '\']) && !preg_match(\'/^[^\@]+@.*\.[a-z]{2,6}$/i\', $this->request->post[\'' . $each_field['name'] . '\'])) {' . "\n" .
	'			$this->error[\'' . $each_field['name'] . '\'] = $this->language->get(\'error_invalid_' . $each_field['name'] . '\');' . "\n" .
	'		}' . "\n";		
	}
	
	if(in_array($each_field['type'], array('date', 'datetime', 'date_added', 'date_modified'))) {
		echo "\r" . '		if (!isset($this->error[\'' . $each_field['name'] . '\']) && !strtotime($this->request->post[\'' . $each_field['name'] . '\'])) {' . "\n" .
	'			$this->error[\'' . $each_field['name'] . '\'] = $this->language->get(\'error_invalid_' . $each_field['name'] . '\');' . "\n" .
	'		}' . "\n";		
	}
}
$validate_fields = ob_get_clean();

ob_start();
foreach ($form_fields as $each_field) {
	
	echo "\r" . '		if (isset($this->error[\'' . $each_field['name'] . '\'])) {' . "\n" .
	'			$this->data[\'error_' . $each_field['name'] . '\'] = $this->error[\'' . $each_field['name'] . '\'];' . "\n" .
	'		} else {' . "\n" .
	'			$this->data[\'error_' . $each_field['name'] . '\'] = \'\';' . "\n" .
	'		}' . "\n\n";
	
}
$form_error_fields = ob_get_clean();

ob_start();
foreach ($form_fields as $each_field) {

	echo "\r" . '		if (isset($this->request->post[\'' . $each_field['name'] . '\'])) {' . "\n" .
	'			$this->data[\'' . $each_field['name'] . '\'] = $this->request->post[\'' . $each_field['name'] . '\'];' . "\n" .
	'		} elseif (isset($' . $module_name_small . '_info)) {' . "\n" .
	'			$this->data[\'' . $each_field['name'] . '\'] = $' . $module_name_small . '_info[\'' . $each_field['name'] . '\'];' . "\n" .
	'		} else {' . "\n" .
	'			$this->data[\'' . $each_field['name'] . '\'] = \'\';' . "\n" .
	'		}' . "\n";
}

foreach ($dropdown_fields as $each_field) {
	echo "\r" . '		$this->data[\'' . $each_field['name'] . 's\'] = $this->'.$model_object.'->get' . str_replace(' ','', $each_field['label']) . 's();'. "\n";
}
$form_request_fields = ob_get_clean();

ob_start();
foreach ($search_fields as $each_field) {	
	echo "\r" .'			if (isset($this->request->get[\'filter_' . $each_field['name'] . '\'])) {' . "\n" .
		 '				$url .= \'&filter_' . $each_field['name'] . '=\' . $this->request->get[\'filter_' . $each_field['name'] . '\'];' . "\n" .
		 '			}'. "\n\n";
}
$filter_request_fields = ob_get_clean();

ob_start();
foreach ($search_fields as $each_field) {	  	
  	echo "\r" .'		if (isset($this->request->get[\'filter_' . $each_field['name'] . '\'])) {' . "\n" .
		'			$filter_' . $each_field['name'] . ' = $this->request->get[\'filter_' . $each_field['name'] . '\'];' . "\n" .
		'		} else {' . "\n" .
		'			$filter_' . $each_field['name'] . ' = null;' . "\n" .
		'		}' . "\n\n";
}
$filter_list_fields = ob_get_clean();

ob_start();
foreach ($search_fields as $each_field) {
	echo "\r" .'			\'filter_' . $each_field['name'] . '\' => $filter_' . $each_field['name'] . ',';
}
$filter_db_fields = ob_get_clean();

ob_start();
foreach ($search_fields as $each_field) {
	echo "\r" .'		$this->data[\'filter_' . $each_field['name'] . '\'] = $filter_' . $each_field['name'] . ';';
}
$filter_data_fields = ob_get_clean();

ob_start();
if(count($search_fields) > 0) {
	echo "\r" .'		$this->data[\'button_filter\'] = $this->language->get(\'button_filter\');';
}
$list_buttons = ob_get_clean();


ob_start();
echo "\r" . '		$display_data = array(\'display\' => \'1\');';
foreach ($dropdown_fields as $each_field) {
	if(preg_match("@_id@", $each_field['name'])) $each_field['name'] = str_replace("_id", "", $each_field['name']);
	echo "\r" . '		$display_' . $each_field['name'] . 's = $this->'.$model_object.'->get' . str_replace(' ','', $each_field['label']) . 's($display_data);';
}
$display_drop_down_fields = ob_get_clean();

ob_start();
foreach ($dropdown_fields as $each_field) {
	if(preg_match("@_id@", $each_field['name'])) $each_field['name'] = str_replace("_id", "", $each_field['name']);
	echo "\r" . '		$this->data[\'' . $each_field['name'] . 's\'] = $this->'.$model_object.'->get' . str_replace(' ','', $each_field['label']) . 's();';
}
$drop_down_fields = ob_get_clean();

$maches_arr = array (
	'/@controller_name@/',
	'/@display_drop_down_fields@/',
	'/@drop_down_fields@/',
	'/@filter_data_fields@/',
	'/@filter_db_fields@/',
	'/@filter_list_fields@/',
	'/@filter_request_fields@/',
	'/@language_path@/',
	'/@model_object@/',
	'/@model_path@/',
	'/@module_name@/',
	'/@module_name_small@/',
	'/@module_table_name@/',
	'/@module_primary_key@/',
	'/@module_path@/',
	'/@form_request_fields@/',
	'/@form_error_fields@/',
	'/@form_entry_fields@/',
	'/@list_buttons@/',
	'/@list_column_fields@/',
	'/@list_db_fields@/',
	'/@list_sort_fields@/',
	'/@validate_fields@/',
	
);

$replace_arr = array (
	$controller_name,
	$display_drop_down_fields,
	$drop_down_fields,
	$filter_data_fields,
	$filter_db_fields,
	$filter_list_fields,
	$filter_request_fields,
	$language_path,
	$model_object,
	$model_path,
	$module_name,
	$module_name_small,
	$module_table_name,
	$module_primary_key,
	$module_path,
	$form_request_fields,
	$form_error_fields,
	$form_entry_fields,
	$list_buttons,
	$list_column_fields,
	$list_db_fields,
	$list_sort_fields,
	$validate_fields,
	
);

//	preg_match_all('/@([^@]+)@/', $controller_file_contents, $maches);
//	sort($maches['1']);
//	print_r(array_unique($maches['1']));
	
$controller_file_contents = preg_replace($maches_arr, $replace_arr, $controller_file_contents);
?>